package de.koller.hydrogen.engine.player;

import de.koller.aframe.plain.IRenderObject;
import de.koller.hydrogen.engine.physics.HMovingObject;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

public class PlayerRender implements IRenderObject {

	private final Player player;
	private BufferedImage texture;

	public PlayerRender( Player player ) {
		this.player = player;
	}

	@Override
	public void render( Graphics2D g ) {
		if( texture == null )
			createTexture();
		
		HMovingObject mo = player.mo;
		AffineTransform a = new AffineTransform();
		a.translate( mo.location.position.x - mo.size.size.x * .5f,
					 mo.location.position.y - mo.size.size.y * .5f );
		g.drawImage( texture, a, null );
	}

	private void createTexture() {
		HMovingObject mo = player.mo;
		texture = new BufferedImage( (int)mo.size.size.x + 1, (int)mo.size.size.y + 1, BufferedImage.TYPE_INT_ARGB );
		Ellipse2D p = new Ellipse2D.Float( 
				0, 0,
				mo.size.size.x, mo.size.size.y );
		
		Graphics2D g = texture.createGraphics();
		g.setColor( new Color( player.color ) );
		g.fill( p );
		
		g.setColor( Color.WHITE );
		g.draw( p );
		
		g.dispose();
	}
	
}
